﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace JCodeManage
{
    public partial class MDIParent1 : Form
    {
        
        JCode.BLL.Category bll = new JCode.BLL.Category();
        public MDIParent1()
        {
            this.IsMdiContainer = true;
            this.WindowState = FormWindowState.Maximized;
            InitializeComponent();
            CreateTree();
            MainForm xx = new MainForm();
            xx.MdiParent = this;
            splitContainer1.Panel2.Controls.Add(xx);
            xx.Show();
        }



        private void CreateTree()
        {
            treeView1.Nodes.Clear();
            TreeNode treeNode = new TreeNode();
            DataSet ds = bll.GetList();
            AddTree("0", treeNode, treeView1, ds);
        }

        public static void AddTree(string ParentID, TreeNode pNode, TreeView treeView, DataSet ds)
        {
            DataView dvTree = new DataView(ds.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[ParentID]='" + ParentID + "'";
            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                //̀添加当前节点的子节点
                Node.Text = Row["CategoryName"].ToString();
                Node.Name = Row["CategoryID"].ToString();
                if (String.IsNullOrEmpty(pNode.Text))
                {
                    treeView.Nodes.Add(Node);
                }
                else
                {
                    pNode.Nodes.Add(Node);
                }
                Node.Expand();
                AddTree(Row["CategoryID"].ToString(), Node, treeView, ds);//递归
            }
        }



        private void toolStripBtnAddCate_Click(object sender, EventArgs e)
        {
            JCodeManage.Category.CategoryEdit ca = new JCodeManage.Category.CategoryEdit(int.Parse(treeView1.SelectedNode.Name),true);

            ca.ShowDialog();
            CreateTree();
        }

        private void toolStripbtncodeadd_Click(object sender, EventArgs e)
        {
            CodeModify form = new CodeModify();
            form.MdiParent = this;
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(form);
            form.Show();
        }
        //双击节点 加载代码列表
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //treeView1.SelectedNode.Text = "doubleclick";
            string cateid = treeView1.SelectedNode.Name;
            CodeList form = new CodeList(int.Parse(cateid),this);
            form.MdiParent = this;
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(form);
            form.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            JCodeManage.Category.CategoryEdit ca = new JCodeManage.Category.CategoryEdit(int.Parse(treeView1.SelectedNode.Name), false);

            ca.ShowDialog();
            CreateTree();
        }

        public void DisplayCodeInfoForm(int Id)
        {
            JCode.Model.CodeInfo model = new JCode.Model.CodeInfo();
            JCode.BLL.Code bll = new JCode.BLL.Code();
            model = bll.GetModel(Id);
            CodeModify form = new CodeModify(model);
            form.MdiParent = this;
            
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(form);
            form.Show();
        }
    }
}
